/**
 * Created by zlj on 2017/1/14.
 */
require("jquery");
require('layout/util');
var $form = require('js-path/formValide.js');
var $alert = require('libs/alert');

$(function () {
    judgeUpdateOrAdd();
    formInit();
    $("#resource_back").on("click",function () {
        window.history.back();
    });
    $("#submit_form").on("click",function () {
        $("#resource_form").submit();
    });
});

function formInit() {
    $form({selector:"#resource_form"},{resourceType:{required:true},resourceName:{required:true},resourceDesc:{required:true},
        resourcePath:{required:true},priority:{required:true,digits:true}},{resourceType:{required:'资源类型不能为空'},
        resourceName:{required:'资源名称不能为空'},resourceDesc:{required:'资源描述不能为空'}, resourcePath:{required:'资源路径不能为空'},
        priority:{required:'优先次序不能为空',digits:'优先次序必须是整数'}},function () { return  $("#resource_form").serializeObject() },function (result) {
                        if(result.code == 200){
                            window.location.href = 'resource-manage';
                        }else {
                            $alert(result.message);
                        }
        }
    );
}

function judgeUpdateOrAdd() {
    var url = window.location.href;
    if(url.indexOf("id=")>0){
        $("#resource_form").attr("action","api/v1/p/mms/authResource/update");
        $('span.name:first').html('修改资源');
        var id = $.getUrlParam('id');
        load(id);
    }
}

function load(id) {
    $.ajax({
        url:'api/v1/p/mms/authResource/load',
        type:'post',
        data:{id:id},
        dataType:'json',
        success:function (result) {
            if(result.code == 200){
                $("#resource_form").setForm(result.data);
            }else{
                $alert(result.message);
            }
        },
        error:function () {
            console.error('接口: api/v1/p/mms/authResource/load 失败')
        }
    })
}